package com.erpanderp.wam.usm.view.projectandassignment;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.servlet.http.HttpSession;

import org.primefaces.component.datatable.DataTable;

import com.erpanderp.wam.ac.model.entity.project.ProjectAssignment;
import com.erpanderp.wam.adm.model.entity.SAPERPModule;
import com.erpanderp.wam.adm.model.service.SAPERPModuleDAO;
import com.erpanderp.wam.usm.model.entity.projectandassignment.EmployeeProject;
import com.erpanderp.wam.usm.model.entity.projectandassignment.Project;
import com.erpanderp.wam.usm.model.entity.timesheet.ProjectAssignmentTimesheetDetail;
import com.erpanderp.wam.usm.model.entity.timesheet.Timesheet;
import com.erpanderp.wam.usm.model.entity.timesheet.TimesheetDetail;
import com.erpanderp.wam.usm.model.entity.user.Employee;
import com.erpanderp.wam.usm.model.entity.user.EmployeeAndReporting;
import com.erpanderp.wam.usm.model.entity.user.WAMUser;
import com.erpanderp.wam.usm.model.service.projectandassignment.EmployeeProjectDAO;
import com.erpanderp.wam.usm.model.service.projectandassignment.ProjectDAO;
import com.erpanderp.wam.usm.model.service.timesheet.ProjectAssignmentTimesheetDetailDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDAO;
import com.erpanderp.wam.usm.model.service.timesheet.TimesheetDetailDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeAndReportingDAO;
import com.erpanderp.wam.usm.model.service.user.EmployeeDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.EmailUtil;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class AssignProjectBean {
	HttpSession session = (HttpSession) FacesContext.getCurrentInstance()
			.getExternalContext().getSession(true);

	ProjectDAO projectDAO = new ProjectDAO();
	EmployeeDAO employeeDAO = new EmployeeDAO();
	EmployeeProjectDAO employeeProjectDAO = new EmployeeProjectDAO();

	private DataTable dataTable;
	private Project project = new Project();
	private SAPERPModuleDAO sapERPModuleDAO = new SAPERPModuleDAO();
	private EmployeeProject employeeProject = new EmployeeProject();
	private WAMUser wamUser = new WAMUser();

	private List<Project> projectsList = new ArrayList<Project>();
	private List<SAPERPModule> modulesList = new ArrayList<SAPERPModule>();
	private Map<String, String> modulesMap = new HashMap<String, String>();

	private String selectedModuleName;
	private Long selectedEmployeeId;

	private SAPERPModule selectedModule = new SAPERPModule();
	private Employee employee = new Employee();

	private boolean FORM_VALID;

	public AssignProjectBean() {
		System.out.println("ASSIGN PROJECT BEAN");
		setWamUser((WAMUser) session
				.getAttribute(CommonLookup.SESSION_ATTRIBUTE_WAMUSER));
		projectsList = projectDAO.getAllProjects();
		System.out.println("projectsList......" + projectsList.size());
		modulesList = sapERPModuleDAO.getSAPERPModules();
		for (SAPERPModule sapERPModule : modulesList) {
			modulesMap.put(sapERPModule.getSAPERPModule(),
					sapERPModule.getSAPERPModule());
		}
		for (Project project : projectsList) {
			project.setModulesMap(modulesMap);
		}
		System.out.println("modulesMap........." + modulesMap.size());
	}

	// GETTING EMPLOYEES BY SELECTING TECHNOLOGY
	public void moduleChangeListener(AjaxBehaviorEvent e) {
		Map<String, Long> employeesMap = new HashMap<String, Long>();
		employeesMap.clear();
		project = (Project) dataTable.getRowData();

		System.out.println("........project......" + project.getProjectName());
		if (project != null && project.getSelectedModuleName() != null
				&& !project.getSelectedModuleName().equals("")) {
			List<Employee> employeesList = employeeDAO
					.getEmployeeByModuleName(project.getSelectedModuleName());
			for (Employee employee : employeesList) {
				employeesMap.put(
						employee.getFirstName() + " " + employee.getLastName(),
						employee.getEmployeeId());
			}
			project.setEmployeesMap(employeesMap);
			System.out.println("employeesMap........." + employeesMap.size());
		}
	}

	// PROJECT ASSIGNMENT TO AN EMPLOYEE ACTION
	public String saveProjectAssignmentAction() {
		// validateForm();
		// if(FORM_VALID){
		
		System.out.println("saveProjectAssignmentAction..................");
		project = (Project) dataTable.getRowData();
		Employee selectedEmployee = employeeDAO.getEmployeeByEmployeeId(project.getSelectedEmployeeId());
		employeeProject.setCreateDate(new Date());
		employeeProject.setAssignedOn(new Date());
		employeeProject.setAssignedTechnology(project.getSelectedModuleName());
		employeeProject.setCreatedBy(wamUser.getEmployee().getFirstName() + " "
				+ wamUser.getEmployee().getLastName());
		employeeProject.setStatus(CommonLookup.STATUS_ACTIVE);
		employeeProject.setEmployee(selectedEmployee);
		employeeProject.setProject(projectDAO.getProjectByProjectID(project
				.getProjectId()));
		
		
		
		EmployeeAndReportingDAO employeeAndReportingDAO = new EmployeeAndReportingDAO();
		EmployeeAndReporting employeeAndReporting = employeeAndReportingDAO.getEmployeeReportingDetail(selectedEmployee.getEmployeeId());
		employeeAndReporting.setProject(project.getProjectName());
		
		
		if (employeeProjectDAO.assignProjectToEmployee(employeeProject) != null && employeeAndReportingDAO.updateEmployeeAndReporting(employeeAndReporting) != null) {
		
			//Timesheet should be updated if already generated.
			String currentMonth = new SimpleDateFormat("MMMM").format(new Date());
			String currentYear = new SimpleDateFormat("yyyy").format(new Date());
			TimesheetDAO timesheetDAO = new TimesheetDAO();
			Timesheet timesheet = timesheetDAO.getTimesheet(currentMonth.toUpperCase(), Integer.parseInt(currentYear), selectedEmployee.getEmployeeId());
			List<ProjectAssignmentTimesheetDetail> projectAssignmentTimesheetDetails = new ArrayList<ProjectAssignmentTimesheetDetail>();
			if(timesheet != null){
				TimesheetDetailDAO timesheetDetailDAO = new TimesheetDetailDAO();
				List<TimesheetDetail> timesheetDetails = timesheetDetailDAO.getTimesheetDetails(timesheet.getTimesheetId());
				for(TimesheetDetail timesheetDetail : timesheetDetails){
					ProjectAssignmentTimesheetDetail assignmentTimesheetDetail = new ProjectAssignmentTimesheetDetail();
					assignmentTimesheetDetail.setCreateDate(new Date());
					assignmentTimesheetDetail.setCreatedBy(wamUser.getEmployee().getFirstName());
					assignmentTimesheetDetail.setEmployeeProject(employeeProject);
					assignmentTimesheetDetail.setStatus(CommonLookup.STATUS_ACTIVE);
					assignmentTimesheetDetail.setTimesheetDetail(timesheetDetail);
					assignmentTimesheetDetail.setTimeSpent("00.00");
					timesheetDetail.getProjectAssignmentTimesheetDetails().add(assignmentTimesheetDetail);
					projectAssignmentTimesheetDetails.add(assignmentTimesheetDetail);
					employeeProject.getProjectAssignmentTimesheetDetails().add(assignmentTimesheetDetail);
				}
				ProjectAssignmentTimesheetDetailDAO assignmentTimesheetDetailDAO = new ProjectAssignmentTimesheetDetailDAO();
				assignmentTimesheetDetailDAO.saveAssignmentTimesheetDetails(projectAssignmentTimesheetDetails);
			}
			
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							employeeProject.getEmployee().getFirstName()
									+ " "
									+ employeeProject.getEmployee()
											.getLastName() + " "
									+ "assigned to" + " "
									+ project.getProjectName() + " "
									+ "successfully.", ""));
			employeeProject = new EmployeeProject();
		} else {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_WARN,
							"Failed to assign Project.", ""));
		}

		System.out.println("In save ProjectAssignment");
		return null;
	}

	// VALIDATION
	private void validateForm() {
		System.out.println("validateForm.....................");
		FORM_VALID = true;
		System.out.println("selectedModuleName1........." + selectedModuleName);
		if (ValidationUtil.isEmpty(selectedModuleName, "Technology", null,
				FacesContext.getCurrentInstance()) != null) {
			FORM_VALID = false;
			System.out.println("selectedModuleName2........."
					+ selectedModuleName);
		}
		// System.out.println("selectedEmployeeId.toString()........."+selectedEmployeeId.toString());
		if (ValidationUtil.isEmpty(selectedEmployeeId.toString(),
				"Employee Name", null, FacesContext.getCurrentInstance()) != null) {
			// System.out.println("selectedEmployeeId.toString()........."+selectedEmployeeId.toString());
			FORM_VALID = false;
			// System.out.println("selectedEmployeeId.toString()........."+selectedEmployeeId.toString());
		}
		System.out.println("Finally FORM_VALID is :::::" + FORM_VALID);
	}

	// GETTERS AND SETTERS
	public List<Project> getProjectsList() {
		return projectsList;
	}

	public void setProjectsList(List<Project> projectsList) {
		this.projectsList = projectsList;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public List<SAPERPModule> getModulesList() {
		return modulesList;
	}

	public void setModulesList(List<SAPERPModule> modulesList) {
		this.modulesList = modulesList;
	}

	public SAPERPModuleDAO getSapERPModuleDAO() {
		return sapERPModuleDAO;
	}

	public void setSapERPModuleDAO(SAPERPModuleDAO sapERPModuleDAO) {
		this.sapERPModuleDAO = sapERPModuleDAO;
	}

	public ProjectDAO getProjectDAO() {
		return projectDAO;
	}

	public void setProjectDAO(ProjectDAO projectDAO) {
		this.projectDAO = projectDAO;
	}

	public Long getSelectedEmployeeId() {
		return selectedEmployeeId;
	}

	public void setSelectedEmployeeId(Long selectedEmployeeId) {
		this.selectedEmployeeId = selectedEmployeeId;
	}

	public Employee getEmployee() {
		return employee;
	}

	public void setEmployee(Employee employee) {
		this.employee = employee;
	}

	public SAPERPModule getSelectedModule() {
		return selectedModule;
	}

	public void setSelectedModule(SAPERPModule selectedModule) {
		this.selectedModule = selectedModule;
	}

	public EmployeeDAO getEmployeeDAO() {
		return employeeDAO;
	}

	public void setEmployeeDAO(EmployeeDAO employeeDAO) {
		this.employeeDAO = employeeDAO;
	}

	public Map<String, String> getModulesMap() {
		return modulesMap;
	}

	public void setModulesMap(Map<String, String> modulesMap) {
		this.modulesMap = modulesMap;
	}

	public String getSelectedModuleName() {
		return selectedModuleName;
	}

	public void setSelectedModuleName(String selectedModuleName) {
		this.selectedModuleName = selectedModuleName;
	}

	public EmployeeProject getEmployeeProject() {
		return employeeProject;
	}

	public void setEmployeeProject(EmployeeProject employeeProject) {
		this.employeeProject = employeeProject;
	}

	public EmployeeProjectDAO getEmployeeProjectDAO() {
		return employeeProjectDAO;
	}

	public void setEmployeeProjectDAO(EmployeeProjectDAO employeeProjectDAO) {
		this.employeeProjectDAO = employeeProjectDAO;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public WAMUser getWamUser() {
		return wamUser;
	}

	public void setWamUser(WAMUser wamUser) {
		this.wamUser = wamUser;
	}

	public boolean isFORM_VALID() {
		return FORM_VALID;
	}

	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}

	public DataTable getDataTable() {
		return dataTable;
	}

	public void setDataTable(DataTable dataTable) {
		this.dataTable = dataTable;
	}
}
